package com.wrx.club_management_backend.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wrx.club_management_backend.common.AjaxResult;
import com.wrx.club_management_backend.dto.MessageUpdateDTO;
import com.wrx.club_management_backend.dto.MsgDTO;
import com.wrx.club_management_backend.entity.Msg;
import com.wrx.club_management_backend.mapper.MsgMapper;
import com.wrx.club_management_backend.service.MsgService;
import com.wrx.club_management_backend.utils.UserHolder;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.nio.cs.ext.MS874;

import javax.jws.Oneway;
import java.util.List;

@Slf4j
@Service
public class MsgServiceImpl extends ServiceImpl<MsgMapper, Msg>implements MsgService {

    @Autowired
    MsgMapper msgMapper;

    /**
     * 发送消息
     * @param dto
     * @return
     */
    @Override
    public AjaxResult send(MsgDTO dto) {
        Msg msg = new Msg();
        BeanUtil.copyProperties(dto, msg);
        if(dto.getType() == 0){
            msg.setTitle("普通聊天");
        }else{
            msg.setTitle("系统消息");
        }
        msg.setStat(0);
        this.save(msg);
        return AjaxResult.success("发送成功");
    }

    /**
     * 获取聊天历史
     * @param friendId
     * @return
     */
    @Override
    public AjaxResult history(Long friendId) {
        if (friendId == null) {
            return AjaxResult.error("好友ID不能为空");
        }
        Long curId = UserHolder.getUser().getId();
        log.warn("userId = {} friendId = {}", curId, friendId);
        List<Msg> msgs = msgMapper.selectList(
                new LambdaQueryWrapper<Msg>()
                        .and(qw -> qw
                                .eq(Msg::getFrom, curId).eq(Msg::getTo, friendId)
                                .or()
                                .eq(Msg::getFrom, friendId).eq(Msg::getTo, curId)
                        )
                        .orderByAsc(Msg::getCreateTime)
        );
        return AjaxResult.success(msgs);
    }

    /**
     * 获取id用户的 系统消息
     * @param id
     * @return
     */
    @Override
    public AjaxResult sysmsg(Long id) {
        List<Msg> list = this.list(
                new LambdaQueryWrapper<Msg>()
                        .and(qw -> qw
                                .ne(Msg::getType, 0)
                                .eq(Msg::getTo, id)
                        )

        );
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult updateStat(MessageUpdateDTO dto) {
        this.update(
                new LambdaUpdateWrapper<Msg>()
                        .eq(Msg::getId, dto.getId())
                        .set(Msg::getStat, dto.getStat())
        );
        return AjaxResult.success("添加成功");
    }
}
